package com.lianniu.mgr.baseService.organization.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.organization.dao.IOrganizationMagDao;
import com.lianniu.mgr.baseService.organization.model.OrganizationMagVo;
import com.lianniu.mgr.baseService.organization.service.IOrganizationMagService;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;



import com.alibaba.fastjson.JSONObject;

@Service
public class OrganizationMagServiceImpl implements IOrganizationMagService {

  private Logger logger = Logger.getLogger(OrganizationMagServiceImpl.class);
  
  @Autowired
  IOrganizationMagDao organizationdao;
  /**
   * 添加组织
   */
  @Override
  @Transactional
  public BaseResult addNewOrganization(OrganizationMagVo vo) {
    // TODO Auto-generated method stub
    logger.info("OrganizationMagServiceImpl addNewOrganization begin.");
    logger.info("addNewOrganization:input data check begin.");
    logger.info("volist:"+JSONObject.toJSONString(vo));
    if(StringUtil.isEmpty(vo.getOrgNo())){
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    if(StringUtil.isEmpty(vo.getOrgName())){
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    logger.info("addNewOrganization:input data check end.");
    vo.setId(StringUtil.getUUID());
    if(StringUtil.isEmpty(vo.getOrgPath())){
      vo.setOrgPath(vo.getId());
    }else{
      String path = vo.getOrgPath()+">"+vo.getId();
      vo.setOrgPath(path);
    }
    int i = organizationdao.addOrganization(vo);
    logger.info("OrganizationMagServiceImpl addNewOrganization end.");
    return BaseResult.success(new Integer(i));
  }

  /**
   * 编辑组织
   */
  @Override
  @Transactional
  public BaseResult editOrganization(OrganizationMagVo vo) {
    // TODO Auto-generated method stub
    logger.info("OrganizationMagServiceImpl editOrganization begin.");
    logger.info("editOrganization data check begin.");
    logger.info("volist:"+JSONObject.toJSONString(vo));
    if(StringUtil.isEmpty(vo.getId())){
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    if(StringUtil.isEmpty(vo.getOrgName())){
      return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
    }
    logger.info("editOrganization data check end.");
    int sta = organizationdao.editOrganization(vo);
    logger.info("OrganizationMagServiceImpl editOrganization end.");
    return BaseResult.success(new Integer(sta));
  }
  /**
   * 批量删除组织
   */
  @Override
  @Transactional
  public BaseResult delOrganization(String[] ids,String modifyUser) {
    // TODO Auto-generated method stub
    logger.info("OrganizationMagServiceImpl delOrganization begin.");
    List<String> tmpLs = new ArrayList<String>();
    logger.info("delOrganization data check begin.");
    logger.info("volist:"+JSONObject.toJSONString(ids));
    for(String id:ids){
      if(StringUtil.isEmpty(id)){
        return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
      }
      tmpLs.add(id);
    }
    logger.info("delOrganization data check end.");
    organizationdao.editOrganizationBatch(tmpLs,modifyUser);
    logger.info("OrganizationMagServiceImpl delOrganization end.");
    return BaseResult.success(JSONObject.toJSONString(tmpLs));
  }
  /**
   * 获取组织列表
   * 
   */
  @Override
  public BaseResult getOrganizationLs(OrganizationMagVo vo) {
    // TODO Auto-generated method stub
    logger.info("OrganizationMagServiceImpl getOrganizationLs begin.");
    if(!StringUtil.isEmpty(vo.getOrgName())){
      String queryName = vo.getOrgName().toLowerCase().replace(" ", "");
      vo.setOrgName(queryName);
    }
    List<OrganizationMagVo> list = organizationdao.getOrgListByOrgInfo(vo);
    String jsonString = JSONObject.toJSONString(list);
    logger.info("OrganizationMagServiceImpl getOrganizationLs end.");
    return BaseResult.success(jsonString);
  }
  /**
   * 获取组织编号(添加组织的时候用)
   */
  @Override
  public BaseResult getOrgNo() {
    // TODO Auto-generated method stub
    logger.info("OrganizationMagServiceImpl getOrgNo begin.");
    List<OrganizationMagVo> list = organizationdao.getOrgListByOrgInfo(new OrganizationMagVo());
    String lastno = "9999";
    if(list.size()>0){
      lastno = list.get(list.size()-1).getOrgNo();
    }
    int num;
    try {
      logger.info("OrganizationMagServiceImpl getOrgNo data check begin.");
      logger.info("lastno:"+lastno);
      num = new Integer(lastno);
      logger.info("OrganizationMagServiceImpl getOrgNo data check end.");
      num++;
    } catch (Exception e) {
      // TODO Auto-generated catch block
      logger.error(e.getMessage());
      return BaseResult.fail(ErrorCode.NumberFormatExceptionRetCode, ErrorCode.NumberFormatExceptionRetInfo);
    }
    logger.info("OrganizationMagServiceImpl getOrgNo end.");
    return BaseResult.success(num);
  }
  /**
   * 根据等级列表获取组织列表
   */
  @Override
  public BaseResult getOrgNameByLevel(String maxLevel) {
    // TODO Auto-generated method stub
    logger.info("OrganizationMagServiceImpl getOrgNameByLevel begin.");
    List<OrganizationMagVo> resultLs = new ArrayList<OrganizationMagVo>();
    List<String> levelLs = new ArrayList<String>();
    if(!StringUtil.isEmpty(maxLevel)){
      int maxL;
      try {
        logger.info("OrganizationMagServiceImpl getOrgNo data check begin.");
        logger.info("maxLevel:"+maxLevel);
        maxL = Integer.parseInt(maxLevel);
        logger.info("OrganizationMagServiceImpl getOrgNo data check end.");
      } catch (NumberFormatException e) {
        // TODO Auto-generated catch block
        logger.error(e.getMessage());
        return BaseResult.exception(e.getMessage());
      } catch (Exception e){
        logger.error(e.getMessage());
        return BaseResult.exception(e.getMessage());
      }
      for(int i=1;i<=maxL;i++){
        levelLs.add(i+"");
      }
      resultLs = organizationdao.getOrgNameByLevelLs(levelLs);
    }else{
      resultLs = organizationdao.getOrgListByOrgInfo(new OrganizationMagVo());
    }
    logger.info("OrganizationMagServiceImpl getOrgNameByLevel end.");
    return BaseResult.success(JSONObject.toJSONString(resultLs));
  }
}
